//
//  PlayListTableCell.swift
//  GhostWorld
//
//  Created by Kam on 2018/5/12.
//  Copyright © 2018 LKGood. All rights reserved.
//

import UIKit

class PlayListTableCell: UITableViewCell {

  // MARK: - IBOutlets
  
  @IBOutlet private weak var containerView: UIView!
  @IBOutlet private weak var nameLabel: UILabel!
  @IBOutlet private weak var audioCountLabel: UILabel!
  @IBOutlet private weak var subTitleRightConstraint: NSLayoutConstraint!
  @IBOutlet private weak var cellArrow: UIImageView!
  
  
  // MARK: - Private vars
  
  
  
  // MARK: - Private methods
  
  
  
  // MARK: - Public vars
  
  private let selectView = UIView()
  
  // MARK: - Lifecycle
  
  override func awakeFromNib() {
    super.awakeFromNib()
    
    contentView.backgroundColor = .clear
    
    containerView.layer.cornerRadius = 4
    containerView.clipsToBounds = true
    
    // selected background view
    let backgroundView = UIView()
    backgroundView.backgroundColor = .clear
    
    selectView.backgroundColor = UIColor(fromRGB: 0x98000C)
    selectView.clipsToBounds = true
    selectView.layer.cornerRadius = 4
    backgroundView.addSubview(selectView)
    selectedBackgroundView = backgroundView
  }
  
  override func layoutSubviews() {
    super.layoutSubviews()
    
    selectView.frame = containerView.frame
  }
  
  override func prepareForReuse() {
    super.prepareForReuse()
    
    nameLabel.text = "--"
  }
  
  override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)
    
    // Configure the view for the selected state
  }
  
  // MARK: - IBActions
  
  
  
  // MARK: - Public methods
  
  func prepare(playlist: Playlist, canDisplayArrow: Bool = false) {
    nameLabel.text = playlist.name ?? "未命名听单"
    
    let count = playlist.getAudioCount()
    audioCountLabel.text = (count > 0 ? "\(count)个声音" : "没有声音")
    
    if canDisplayArrow {
      cellArrow.isHidden = false
      subTitleRightConstraint.constant = 36
    } else {
      cellArrow.isHidden = true
      subTitleRightConstraint.constant = 16
    }
  }
}

extension PlayListTableCell {
  static let defaultHeight: CGFloat = 70
}
